package models

import (
	"fresh-food/inits/initialize"
	"gorm.io/gorm"
)

type User struct {
	gorm.Model
	Mobile   string `gorm:"type:varchar(20);not null;index:ip_idx"`
	Password string `gorm:"type:varchar(20);not null;index:ip_idx"`
}

func (u *User) FindUserByMobile(mobile string) (user *User, err error) {
	err = initialize.DB.Where("mobile=?", mobile).Find(&user).Error
	if err != nil {
		return nil, err
	}
	return user, nil
}

type Product struct {
	gorm.Model
	Name  string  `gorm:"type:varchar(20);not null;index:ip_idx"`
	Price float64 `gorm:"type:decimal(10,2);not null;index:ip_idx"`
	Stock int     `gorm:"type:int;not null;index:ip_idx"`
}

type ProductAttribute struct {
	gorm.Model
	Variety string `gorm:"type:varchar(20);not null;index:ip_idx"`
	Size    string `gorm:"type:varchar(20);not null;index:ip_idx"`
	Color   string `gorm:"type:varchar(20);not null;index:ip_idx"`
}

type Order struct {
	gorm.Model
	UserId    int     `gorm:"type:int;not null;index:ip_idx"`
	ProductId int     `gorm:"type:int;not null;index:ip_idx"`
	OrderSn   string  `gorm:"type:varchar(20);not null;index:ip_idx"`
	Num       int     `gorm:"type:int;not null;index:ip_idx"`
	Total     float64 `gorm:"type:decimal(10,2);not null;index:ip_idx"`
	Status    int     `gorm:"type:int;not null;index:ip_idx"`
	Pay       int     `gorm:"type:int;not null;index:ip_idx"`
}

func (o Order) FindOrder(id uint) (order []*Order, err error) {
	err = initialize.DB.Model(&order).Find(&order).Error
	if err != nil {
		return nil, err
	}
	return
}
